package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.insert(strategyComment);

    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment selectOne(String id) {
        StrategyComment strategyComment = strategyCommentRepository.findById(id).orElse(null);
        //StrategyComment strategyComment = byId.get();
        return strategyComment;
    }

    @Override
    public List<StrategyComment> selectAll() {
        List<StrategyComment> all = strategyCommentRepository.findAll();
        return all;
    }

    //@Override
    //public List<StrategyComment> getDestByRegionId(String rid) {
    //    //1、通过区域id查询区域对象
    //    Region region = regionService.selectOne(rid);
    //    //2、从区域对象上获取目的地id的集合
    //    List<String> refIds = region.getRefIds();
    //    //3、将目的地id的集合转换成目的地名称的集合
    //    List<StrategyComment> strategyComments = strategyCommentRepository.findByIdIn(refIds);
    //    return strategyComments;
    //}

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject q) {
        //1、创建查询对象
        Query query=new Query();

        //拼接攻略id的条件
        if(StringUtils.hasLength(q.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(q.getStrategyId()));
        }
        //2、判断keyword是否为空，若不为空，则进行条件的拼接
        if(StringUtils.hasLength(q.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(q.getKeyword()));//进行拼接高级查询条件时需要使用regex
        }
        //判断父亲id长度是否为空，若为空，拼接成null
        if(!StringUtils.hasLength(q.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(null));
        }else{
            query.addCriteria(Criteria.where("parentId").is(q.getParentId()));
        }
        //3、添加分页语句
        Pageable pageable = PageRequest.of(q.getCurrentPage()-1, q.getPageSize(), Sort.Direction.DESC, "_id");
        Page<StrategyComment> page = DBHelper.query(q,query, StrategyComment.class, template, pageable);
        return page;
    }

    @Override
    public List<StrategyComment> queryByCommen(String id) {
        List<StrategyComment> list=strategyCommentRepository.queryByStrategyId(id);
        return list;
    }

    //通过攻略id获取对应的攻略内容
    @Override
    public List<StrategyComment> queryByStrategyId(String strategyId) {
        List<StrategyComment> strategyComments = strategyCommentRepository.queryByStrategyId(strategyId);
        return strategyComments;
    }

    //处理点赞功能
    @Override
    public StrategyComment commentThumb(String cid) {
        //创建数据结构来储存评论用户的id
        //通过攻略评论的id来获取当前登录用户的id
        StrategyComment strategyComment = this.selectOne(cid);
        String userId = strategyComment.getUserId();
        //得到数据结构
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断评论用户的id是否存在于这个集合当中，若存在，移除集合中当前登录用户的id，点赞数减一
        if(thumbuplist.contains(userId)){
            thumbuplist.remove(userId);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
        }else{
            //若不存在，将该用户的id储存到该集合中，点赞数加一
            thumbuplist.add(userId);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
        }
        strategyComment.setThumbuplist(thumbuplist);
        this.update(strategyComment);
        return strategyComment;
    }



}
